#include "GamePage.h"
#include <QTimer>
#include <QWidget>
#include <QRandomGenerator>


const QString fruitImages[5] = {
    ":/D:/photo/fruit0.png"  ,
    ":/D:/photo/fruit1.png",
    ":/D:/photo/fruit2.png",
    ":/D:/photo/fruit3.png",
 ":/D:/photo/fruit4.png"
};
QPixmap getRandomFruitImage() {
    // 从水果图片数组中随机选择一张图片
    int randomIndex = QRandomGenerator::global()->bounded(0, 5);
    return QPixmap(fruitImages[randomIndex]);
}




GamePage::GamePage(QWidget *parent) : QWidget(parent) {
    setWindowTitle("游戏");//设置标题
    setFixedSize(800, 600);
    Game_Init();
    Pause_Init();
    QTimer::singleShot(1500, this, [=]() {
        timer1 = startTimer(15);
        timer3 = startTimer(40);
        game_start = true;
    });
}

void GamePage::Pause_Init() {
    Pause = new GamePause();//初始化暂停窗口
    MyPushButton *btn_continue = new MyPushButton(":/D:/photo/continueGame.png");//添加继续按钮
    btn_continue->setParent(Pause);
    btn_continue->setFixedSize(150, 75);
    btn_continue->setIconSize(QSize(150, 75));
    btn_continue->move(20, 10);
    connect(btn_continue, &QPushButton::clicked, this, [=]() {
        btn_continue->zoom1();//加载动画
        btn_continue->zoom2();
        QTimer::singleShot(500, this, [=]() {
            timer1 = startTimer(15);
            timer3 = startTimer(40);
            role->walk_state = 0;//初始化role行走状态
            key = "null";
            Pause->close();
        });
    });
    MyPushButton *initGame = new MyPushButton(":/D:/photo/initGame.png");//添加初始化窗口
    initGame->setParent(Pause);
    initGame->setFixedSize(150, 75);
    initGame->setIconSize(QSize(150, 75));
    initGame->move(20, 90);
    connect(initGame, &QPushButton::clicked, this, [=]() {
        initGame->zoom1();
        initGame->zoom2();
        QTimer::singleShot(500, this, [=]() {
            Pause_Game_Init();//游戏初始化
            Pause->close();
            QTimer::singleShot(1500, this, [=]() {
                timer1 = startTimer(15);//开启定时器
                timer3 = startTimer(40);
                game_start = true;
            });
        });
    });
    MyPushButton *btn_exit = new MyPushButton(":/D:/photo/start.png");//添加离开按钮
    btn_exit->setParent(Pause);
    btn_exit->setFixedSize(150, 75);
    btn_exit->setIconSize(QSize(150, 75));
    btn_exit->move(20, 170);
    connect(btn_exit, &QPushButton::clicked, this, [=]() {
        btn_exit->zoom1();
        btn_exit->zoom2();
        QTimer::singleShot(500, this, [=]() {
            this->close();
        });
    });
}

void GamePage::timerEvent(QTimerEvent *event) {
    if (event->timerId() == timer1 && role->is_die) {
        role->Role_die();
        Die_Init();
        update();
        return;
    }
    if (event->timerId() == timer1) {
      role->Role_Move(key);
        role->Jump_And_Down();
        Jump_Collision();
        Move_Collision();
        brick->ShatterState();
        mushroom->Move_state();
        ghost->Ghost_Move();
        Die_Init();
        Fall_Down(role->y);
        update();
    }

    if (event->timerId() == timer2) {
        role->Role_Move(key);
    }

}

void GamePage::keyPressEvent(QKeyEvent *event) {
    if (!role->is_die) {
        switch (event->key()) {
        case Qt::Key_Right:
            role->direction = key = "right";
            break;
        case Qt::Key_Left:
            role->direction = key = "left";
            break;
        case Qt::Key_Z:
            timer2 = startTimer(25);
            is_kill_timer2 = false;
            break;
        case Qt::Key_Space:
            role->is_jump = true;
            break;
        case Qt::Key_B:
            if (game_start) {
                killTimer(timer1);
                if (is_kill_timer2) {
                    killTimer(timer2);
                }
                killTimer(timer3);
                Pause->setParent(this);
                Pause->open();
            }
            break;
        case Qt::Key_C:
            if (role->life < 8) {
                role->life++;
            }
        case Qt::Key_X:
            if (!is_press_x && !role->is_jump && role->is_jump_end && role->colour == 3) {
                is_press_x = true;
            }
            break;
        }
    }
}

void GamePage::keyReleaseEvent(QKeyEvent *event) {
    if (!role->is_die) {
        switch (event->key()) {
        case Qt::Key_Right:
            role->walk_state = 0;
            key = "null";
            break;
        case Qt::Key_Left:
            role->walk_state = 0;
            key = "null";
            break;
        case Qt::Key_Z:
            is_kill_timer2 = true;
            killTimer(timer2);
            break;
        case Qt::Key_Space:
            role->is_jump = false;
            role->is_space_release = true;
            break;
        case Qt::Key_X:
            is_press_x = false;
            break;
        }
    }
}

void GamePage::paintEvent(QPaintEvent *) {
    QPainter painter(this);
    if (!game_start) {
        painter.drawPixmap(0, 0, 800, 600, QPixmap(":/D:/photo/blackground2.png"));
        painter.drawPixmap(300, 250, 40, 40, QPixmap(":/D:/photo/life.png"));
        painter.setPen(QColor(255, 255, 255));
        QFont font;
        font.setPointSize(35);
        painter.setFont(font);
        painter.drawText(360, 280, "x");
        painter.drawText(220, 32, QString::number(time, 'f', 1));
        painter.drawText(600, 30, "coin:");
        font.setPointSize(45);
        painter.setFont(font);
        painter.drawText(400, 287, QString::number(role->life));
        return;
    }
    painter.drawPixmap(0, 0, 800, 550, QPixmap(":/D:/photo/sky.png"));//画背景
    painter.drawPixmap(230, 10, QPixmap(":/D:/photo/watermelon.png"), 0, 0, 30, 30);
    painter.drawPixmap(380, 10, 30, 30, QPixmap(":/D:/photo/score.png"));

    painter.drawText(430, 38, QString::number(score));
    for (int i = 1; i <= role->life; i++) {
        painter.drawPixmap(800 - i * 35, 10, 30, 30, QPixmap(":/D:/photo/life.png"));
    }
    painter.drawText(50, 38, QString::number(time, 'f', 1));
    painter.drawPixmap(0, 500, QPixmap(":/D:/photo/ground.png"), role->ground_state, 0, 800, 45);//画地板
    if (role->x > 7800) {
        QVector < QVector < int >> ::iterator
            it = exit->m.begin()->begin();
        painter.drawPixmap(*it->begin() - role->x, *(it->begin() + 1), 200, 200, QPixmap(":/D:/photo/castlei.png"));
    }
    if (mushroom->mushroom_state != 0) {
        painter.drawPixmap(mushroom->mushroom_x - role->x, mushroom->mushroom_y, 40, 40,
                           QPixmap(":/D:/photo/mushroom" + QString::number(role->colour) + ".png"));
    }

    for (QVector < QVector < int >> ::iterator it = brick->m.begin()->begin(); it != brick->m.begin()->end();
         it++)
    {
        if (*(it->begin()) - role->x > -50 && *(it->begin()) - role->x < 800 && *(it->begin() + 2) == 1) {
            painter.drawPixmap(*(it->begin()) - role->x, *(it->begin() + 1), 50, 40, getRandomFruitImage());
        }
    if (brick->shatter_state > 0) {
        painter.drawPixmap(brick->left_shatter_x - role->x, brick->left_shatter_y - 20, 30, 30,
                           QPixmap(":/D:/photo/brick2.png"));
        painter.drawPixmap(brick->left_shatter_x - role->x, brick->left_shatter_y + 50, 30, 30,
                           QPixmap(":/D:/photo/brick2.png"));
        painter.drawPixmap(brick->right_shatter_x - role->x, brick->right_shatter_y - 20, 30, 30,
                           QPixmap(":/D:/photo/brick3.png"));
        painter.drawPixmap(brick->right_shatter_x - role->x, brick->right_shatter_y + 50, 30, 30,
                           QPixmap(":/D:/photo/brick3.png"));
    }
    for (QVector < QVector < int >> ::iterator it = ghost->m.begin()->begin(); it != ghost->m.begin()->end();
         it++)
    {
        if (*(it->begin()) - role->x > -80 && *(it->begin()) - role->x < 800 && *(it->begin() + 2) != 0) {
            painter.drawPixmap(*(it->begin()) - role->x, *(it->begin() + 1), 40, 40,
                               QPixmap(":/D:/photo/ghost.png"));
        } else if (*(it->begin()) - role->x > -80 && *(it->begin()) - role->x < 800 && *(it->begin() + 2) == 0 &&
                   *(it->begin() + 4) == 1) {
            painter.drawPixmap(*(it->begin()) - role->x, *(it->begin() + 1) + 20, 40, 20,
                               QPixmap(":/D:/photo/ghost.png"));
        }
    }
    if (role->is_die) {
        painter.setBrush(QBrush(Qt::red));
        painter.drawEllipse(role->map_x, role->y, 45, 45); // 红色圆形
    } else if (!role->is_die && role->invincible_state % 2 == 0 && !is_win) {
        painter.setBrush(QBrush(Qt::blue));
        painter.drawEllipse(role->map_x, role->y, 45, 45); // 蓝色圆形
    }
    QPixmap pixmap("path/to/sky.png");
    if (pixmap.isNull()) {
        qDebug() << "Image load failed!";
    }

    }}

void GamePage::Game_Init() {
    role = new Role;
    brick = new Brick;
    mushroom = new MushRoom;
    ghost= new Ghost;
    exit = new Exit;
    key = "null";
    is_press_x = false;
    is_win = false;
    score = 0;
    time = 300.0;
    is_kill_timer2 = true;
    game_start = false;
    ghost->Ghost_State(role,  brick);


}

void GamePage::Pause_Game_Init() {
    key = "null";
    score = 0;
    time = 300.0;
    is_press_x = false;
    is_kill_timer2 = true;
    game_start = false;
    role->Role_Init();
    brick->BrickInit();
    mushroom->MushRoom_Init();
   ghost->Ghost_Init();
    ghost->Ghost_State(role, brick);
  }

void GamePage::Jump_Collision() {
    if (role->height - role->distance <= 0) {
        return;
    }
    for (QVector < QVector < int >> ::iterator it = brick->m.begin()->begin(); it != brick->m.begin()->end();
         it++)
    {
        if (*it->begin() - role->x - 300 >= -30 && *it->begin() - role->x - 300 <= 30 &&
            *(it->begin() + 1) - role->y + 40 >= -10 && *(it->begin() + 1) - role->y + 40 <= 20 &&
            *(it->begin() + 2) == 1) {
            score += 5;
            brick->BrickShatter(it);
            *(it->begin() + 2) = 0;
            role->y = *(it->begin() + 1) + 40;
           role->height = role->distance;
            return;
        }
    }

    }

void GamePage::Move_Collision() {

    for (QVector < QVector < int >> ::iterator it = brick->m.begin()->begin(); it != brick->m.begin()->end();
         it++)
    {
        if (*it->begin() - role->x - 300 >= 35 && *it->begin() - role->x - 300 <= 40 &&
            *(it->begin() + 1) > role->y - 35 && *(it->begin() + 1) < role->y + 35 && role->direction == "right" &&
            *(it->begin() + 2) == 1) {
            role->can_move = false;
            return;
        } else if (*it->begin() - role->x - 300 >= -40 && *it->begin() - role->x - 300 <= -35 &&
                   *(it->begin() + 1) > role->y - 35 && *(it->begin() + 1) < role->y + 35 &&
                   role->direction == "left" && *(it->begin() + 2) == 1) {
            role->can_move = false;
            return;
        }
    }


    QVector < QVector < int >> ::iterator
        it = exit->m.begin()->begin();
    if (*it->begin() - role->x - 300 >= -60 && *it->begin() - role->x - 300 <= -20 &&
        *(it->begin() + 1) < role->y - 100 && *(it->begin() + 1) > role->y - 200) {

        is_win = true;
        Game_Win();
    }
    role->can_move = true;
}

void GamePage::Fall_Down(int &y) {
    qDebug() << role->distance;
    if (role->height - role->distance < 0) {
        if (y > 455) {
            y = 455;
            role->is_jump_end = true;
            role->distance = 0;
            role->height = 20;
            return;
        }
        for (QVector < QVector < int >> ::iterator it = brick->m.begin()->begin(); it != brick->m.begin()->end();
             it++)
        {
            if (*it->begin() - role->x - 300 >= -30 && *it->begin() - role->x - 300 <= 30 &&
                *(it->begin() + 1) <= y + 50 && *(it->begin() + 1) >= y + 25 && *(it->begin() + 2) == 1) {
                y = *(it->begin() + 1) - 45;
               role->is_jump_end = true;
               role->height = role->distance = 20;
                return;
            }
        }



    }
}

void GamePage::Die_Init() {
    if (role->is_die && key != "null") {
        role->walk_state = 0;
        key = "null";
    }
    if (role->invincible_state == 100) {
        role->is_invincible = false;
        role->invincible_state = 0;
    }
    if (role->is_invincible) {
       role->invincible_state += 1;
    }
    if (role->y > 500) {
        role->y = 455;
        role->life--;
        killTimer(timer3);
        killTimer(timer1);
        game_start = false;
        QTimer::singleShot(1500, this, [=]() {
            role->is_die = false;
            role->is_invincible = true;
            timer1 = startTimer(15);//开启定时器
            timer3 = startTimer(40);
            game_start = true;
            role->die_state = 0;
            role->die_pix_state = -50;
        });
    }
}

void GamePage::Game_Win() {
    killTimer(timer1);
    killTimer(timer3);
    QTimer::singleShot(1000, this, [=]() {
        game_start = false;
        update();
    });
    QTimer::singleShot(1000, this, [=]() {
        startTimer(timer1);
        startTimer(timer3);
    });

}
